package com.example.my_invitation_service.controllers;


import com.example.my_invitation_service.entity.Users;
import com.example.my_invitation_service.services.UsersService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

@RestController
@RequestMapping(value = "/User")
public class UsersControllers {

    @Autowired
    private UsersService usersService;

    /**
     * 获取用户信息
     * @param id
     * @return
     */
    @GetMapping("/getUser")
    public Map<String,Object> getUsers(String id){

        Users us = usersService.getById(id);
        Optional<Users> op = Optional.ofNullable(us);

        Map<String, Object> map = null;
        if(op.isPresent()){
            map = new HashMap<>();
            map.put("code","200");
            map.put("result",us);
        }else{
            map = new HashMap<>();
            map.put("code", "500");
        }

        return map;
    }

    /**
     * 保存用户信息
     * @param us
     * @return
     */
    @PostMapping("/addUser")
    public Map<String, Object> addUsers(@RequestBody Users us){

        Map<String, Object> map = null;
        if(usersService.save(us)){
            map = new HashMap<>();
            map.put("code","200");
            map.put("result",us);
        }else{
            map = new HashMap<>();
            map.put("code", "500");
        }

        return map;
    }
}
